# encoding:utf-8
from __future__ import unicode_literals
from django.contrib import admin
from models import HookModel,CarWashLocalModel,UserCarModel,WashSessionModel,CustomerModel,OrderModel,\
     WXOrderModel,MealModel,CarModelModel,CarBrandModel,ZKOtherLog,ZKWashLog
from helpers.director.admin_ex import field

# import sys  
# reload(sys) 
# sys.setdefaultencoding('utf8') 

# Register your models here.
class HookAdmin(admin.ModelAdmin):
    list_display=('method','path','update_at')

class CustomAdmin(admin.ModelAdmin):
    list_display=('nick_name','get_user_name','mobile')
    
    # readonly_fields=('get_user_name',)
    
    @field('用户名')
    def get_user_name(self,obj):
        if obj.user:
            return obj.user.username
        else:
            return ''

class MealAdmin(admin.ModelAdmin):
    list_display=('name','label')

class WXOrderAdmin(admin.ModelAdmin):
    list_display=('no','pay')

class OrderAdmin(admin.ModelAdmin):
    list_display=('no','get_custom','status','meal')
    
    @field('昵称')
    def get_custom(self,obj):
        if obj.car and obj.car.user:
            return obj.car.user.customermodel.nick_name
        else:
            return '---'

class CarbrandAdmin(admin.ModelAdmin):
    list_display=('name','parent','cls','kind')
    
class CarmodelAdmin(admin.ModelAdmin):
    list_display=('brand','name','min_price','max_price')

class ZKWashAdmin(admin.ModelAdmin):
    list_display=('zk','session','code','update_at')

class ZKOtherAdmin(admin.ModelAdmin):
    list_display=('zk','code','update_at')

class UserCarAdmin(admin.ModelAdmin):
    list_display=('car_no','user','get_customer')
    
    def get_customer(self,obj):
        if hasattr(obj.user,'customermodel'):
            return obj.user.customermodel
        else:
            return '---'
    
admin.site.register(HookModel,HookAdmin)
admin.site.register(CarWashLocalModel)
admin.site.register(UserCarModel,UserCarAdmin)
admin.site.register(WashSessionModel)
admin.site.register(CustomerModel,CustomAdmin)
admin.site.register(OrderModel,OrderAdmin)
admin.site.register(MealModel,MealAdmin)
admin.site.register(WXOrderModel,WXOrderAdmin)
admin.site.register(CarBrandModel,CarbrandAdmin)
admin.site.register(CarModelModel,CarmodelAdmin)
admin.site.register(ZKOtherLog,ZKOtherAdmin)
admin.site.register(ZKWashLog,ZKWashAdmin)
